﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Enter.Common
{
    public sealed class SafeString
    {
        private static char[] constant = new char[] { 
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
            'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 
            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
         };

        public static string CheckString(string key, int x)
        {
            if (key.Length != x)
            {
                if (key.Length > x)
                {
                    key = key.Substring(x);
                    return key;
                }
                StringBuilder builder = new StringBuilder();
                builder.Append(key);
                for (int i = key.Length; i < x; i++)
                {
                    builder.Append(i);
                }
                key = builder.ToString();
            }
            return key;
        }

        public static string GenerateRandom(int Length)
        {
            StringBuilder builder = new StringBuilder(0x3e);
            Random random = new Random();
            for (int i = 0; i < Length; i++)
            {
                builder.Append(constant[random.Next(0x3e)]);
            }
            return builder.ToString();
        }

        public static string GetNumRandom(int Length)
        {
            StringBuilder builder = new StringBuilder(10);
            char[] chArray = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            Random random = new Random();
            for (int i = 0; i < Length; i++)
            {
                builder.Append(constant[random.Next(10)]);
            }
            return builder.ToString();
        }

        public static bool IsNull(string value)
        {
            return ((value == null) || (value.Trim().Length == 0));
        }

        public static bool IsNumber(string value)
        {
            if (IsNull(value))
            {
                return false;
            }
            foreach (char ch in value)
            {
                if (!char.IsNumber(ch))
                {
                    return false;
                }
            }
            return true;
        }

        public static string StrCutHtml(string Html, int len, bool IsMoveHtml)
        {
            if (IsNull(Html))
            {
                return "";
            }
            string str = Html;
            if (IsMoveHtml)
            {
                str = StrMoveHtml(Html);
            }
            return StrCutHtmlx(Html, len);
        }

        private static string StrCutHtmlx(string inputString, int len)
        {
            string s = inputString.Trim();
            ASCIIEncoding encoding = new ASCIIEncoding();
            int num = 0;
            string str2 = "";
            byte[] bytes = encoding.GetBytes(s);
            if ((bytes.Length * 2) > len)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] == 0x3f)
                    {
                        num += 2;
                    }
                    else
                    {
                        num++;
                    }
                    try
                    {
                        str2 = str2 + s.Substring(i, 1);
                    }
                    catch
                    {
                        return str2;
                    }
                    if (num > len)
                    {
                        return str2;
                    }
                }
                return str2;
            }
            return s;
        }

        private static string StrMoveHtml(string orgHtml)
        {
            string str = orgHtml;
            return str.Replace("&lt;", "<").Replace("&gt;", ">");
        }

        public static string StrMoveHtmlBadTag(string orgHtml)
        {
            return Regex.Replace(Regex.Replace(StrMoveHtml(orgHtml), "<(iframe|meta|script)(.*?)>((.|\n)*?)</(iframe|meta|script|)>", "", RegexOptions.IgnoreCase), "<(meta)(.*?)>", "", RegexOptions.IgnoreCase);
        }

        public static string StrMoveHtmlTag(string orgHtml)
        {
            return Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(StrMoveHtml(orgHtml), "<(select|option|script|style)(.*?)>((.|\n)*?)</(select|option|script|style)>", "", RegexOptions.IgnoreCase), "&(nbsp|quot|copy);", " ", RegexOptions.IgnoreCase), @"<([\s\S])+?>", " ", RegexOptions.IgnoreCase), "&|&lt;", " ", RegexOptions.IgnoreCase), @"\s+|&nbsp;+", " ", RegexOptions.IgnoreCase).Replace("'", "’").Trim();
        }

        public static string StrMoveSymbol(string html)
        {
            return StrMoveHtml(html).Replace("&quot;", "\"");
        }

        public static string ToBaseReplace(string txt)
        {
            txt = txt.Replace("\r", "");
            txt = txt.Replace("\n", "");
            txt = txt.Replace("\t", "");
            return txt;
        }
    }

}
